<?php

include("global.php");

$pagetitle=" - Administration - Smilies";
$links=" > Administration > Smilies";

include ("header.php");

include ("admin_header.php");

if($userloggedinarray[15]!=="administrator"){
	unset($tplcontent);
   $tplcontent['notice']."Must be logged in as administrator to use control panel!";
    echo template('notice',$tplcontent);
}else{

if(!$addsmilie&&!isset($editsmilie)&&!isset($deletesmilie)&!$addsmilie2&&!isset($editsmilie2)){

	unset($tplcontent);
 $smiliearray=getdata("$configarray[5]/smilies.php");

   for($n=0;$n<count($smiliearray);$n++){
    $linearray=explode("\t",$smiliearray[$n]);

    if(count($linearray)>1){

    $tplcontent['smilies'].="<b>$linearray[0] + $linearray[1] + <img src=\"images/smilies/$linearray[1]\"></b>\n";
    $tplcontent['smilies'].=" | <a href=\"admin_smilies.php?editsmilie=$n\">Edit</a>";
    $tplcontent['smilies'].=" | <a href=\"admin_smilies.php?deletesmilie=$n\">Delete</a>";

    $tplcontent['smilies'].="<br><br>";
    }

   }//end smilie loop

  $tplcontent['addnew'].="<a href=\"admin_smilies.php?addsmilie=1\">Add new</a>";

  echo template('adminsmiliesview',$tplcontent);
 }

 if(isset($editsmilie)){
 unset($tplcontent);

 $smiliearray=getdata("$configarray[5]/smilies.php");
 $linearray=explode("\t",$smiliearray[$editsmilie]);

 $tplcontent['formheader'].="<form action=\"admin_smilies.php\" method=post>";
 $tplcontent['formheader'].="<input type=hidden name=\"editsmilie2\" value=\"$editsmilie\" size=40>";

 $tplcontent['smilieid'].="$editsmilie";

 $tplcontent['smilietext'].="<input type=text name=\"smilietext\" size=5 value=\"$linearray[0]\" class=\"forminput\">";

 $smilieimagesarray=listfilesext("images/smilies");
 @sort($smilieimagesarray);

 $tplcontent['smiliesimageslist'].="<select size=1 name=\"smilieimage\" class=\"forminput\">\n";

 for($n=0;$n<count($smilieimagesarray);$n++){
  if($smilieimagesarray[$n]==$linearray[1]){
   $tplcontent['smiliesimageslist'].="<option value=\"$smilieimagesarray[$n]\" selected>$smilieimagesarray[$n]</option>";
  }else{
   $tplcontent['smiliesimageslist'].="<option value=\"$smilieimagesarray[$n]\">$smilieimagesarray[$n]</option>";
  }
 }

 $tplcontent['smiliesimageslist'].="</select>";

 $tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Update\" class=\"formbutton\">";

 $tplcontent['formfooter'].="</form>";
 
 echo template('adminsmiliesedit',$tplcontent);
 }

 if(isset($editsmilie2)){

  if(trim($smilietext)==""){
	unset($tplcontent);
    $tplcontent['notice'].="Smilie text cannot be empty!";
    echo template('notice',$tplcontent);
  }else{
   writedata("$configarray[5]/smilies.php","$smilietext\t$smilieimage",$editsmilie2);

	unset($tplcontent);
    $tplcontent['notice'].="Smilie edited - <a href=\"admin_smilies.php\">Back to listing</a>";
    echo template('notice',$tplcontent);
  }

 }

 if($addsmilie=="1"){
 unset($tplcontent);

 $tplcontent['formheader'].="<form action=\"admin_smilies.php\" method=post>";
 $tplcontent['formheader'].="<input type=hidden name=\"addsmilie\" value=\"2\" size=40>";

 $tplcontent['smilietext'].="<input type=text name=\"smilietext\" size=5 class=\"forminput\">";

 $smilieimagesarray=listfilesext("images/smilies");

 $tplcontent['smiliesimageslist'].="<select size=1 name=\"smilieimage\" class=\"forminput\">\n";

 for($n=0;$n<count($smilieimagesarray);$n++){
  $tplcontent['smiliesimageslist'].="<option value=\"$smilieimagesarray[$n]\">$smilieimagesarray[$n]</option>";
 }

 $tplcontent['smiliesimageslist'].="</select>";

 $tplcontent['submitbutton'].="<input type=submit name=\"submit\" value=\"Add smilie\" class=\"formbutton\">";
 
 $tplcontent['formfooter'].="</form>";

 echo template('adminsmiliesadd',$tplcontent);
 }

 if($addsmilie=="2"){

  if(trim($smilietext)==""){
	unset($tplcontent);
    $tplcontent['notice'].="Smilie text cannot be empty!";
    echo template('notice',$tplcontent);
  }else{
   $smiliesarray=getdata("$configarray[5]/smilies.php");
   $end=count($smiliesarray);
   writedata("$configarray[5]/smilies.php","$smilietext\t$smilieimage",$end);

	unset($tplcontent);
    $tplcontent['notice'].="Smilie added - <a href=\"admin_smilies.php\">Back to listing</a>";
    echo template('notice',$tplcontent);
  }

 }

 if(isset($deletesmilie)){
 deletedata("$configarray[5]/smilies.php",$deletesmilie);

	unset($tplcontent);
    $tplcontent['notice'].="Smilie $deletesmilie deleted - <a href=\"admin_smilies.php\">Back to listing</a>";
    echo template('notice',$tplcontent);
 }

}
include("admin_footer.php");

include("footer.php");

?>
